package autoteam.ui.tabs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TableModelEvent;

import autoteam.League;
import autoteam.Local;
import autoteam.tables.DNDTable;
import autoteam.tables.DraftTeamsTableModel;
import autoteam.tables.TableSorter;

public class DraftTeamsTab extends TabPanel
{
	/*
	 * This panel is used to generate the Teams Table. Very Important!
	 */
	private static final long serialVersionUID = 3083215492982188785L;

	private DraftTeamsTableModel allocateModel;
	public static int showNightFlag = -1;

	public DraftTeamsTab(final League doc)
	{
		// create the table
		// 
		allocateModel = new DraftTeamsTableModel(doc);
		TableSorter sorter = new TableSorter(allocateModel);
		final DNDTable table = new DNDTable(sorter);
		// sorter.addMouseListenerToHeaderInTable(table);
		sorter.setTableHeader(table.getTableHeader());

		table.setCellSelectionEnabled(true);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		JScrollPane tableScrollPane = new JScrollPane(table);
		
		// The radio buttons
		final JCheckBox checkBox = new JCheckBox("Allow carpools");
		checkBox.setSelected(doc.getDoCarPool());
		checkBox.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent e)
			{
				doc.setDoCarPool(e.getStateChange() == ItemEvent.SELECTED);
			}
		});

		// set the formatting for the columns
		table.formatColumns();

		// the buttons
		// Create the allocate button
		JButton allocateButton = new JButton("Draft Teams");
		allocateButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (table.isEditing())
				{
					table.getCellEditor().stopCellEditing();
				}

				doc.draftTeams();
				allocateModel.fireTableDataChanged();
				allocateModel.fireTableChanged(new TableModelEvent(
						allocateModel, TableModelEvent.HEADER_ROW));
				table.formatColumns();
			}
		});
		// create the export button
		JButton exportButton = new JButton("Export Teams");
		exportButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (table.isEditing())
				{
					table.getCellEditor().stopCellEditing();
				}
				doc.exportTeams();
			}
		});

		// create the CheckTeams button
		JButton checkButton = new JButton("Check Teams");
		checkButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (table.isEditing())
				{
					table.getCellEditor().stopCellEditing();
				}
				doc.checkTeams();
			}
		});

		// For layout purposes, put the buttons in a separate panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(checkBox);
		buttonPanel.add(allocateButton);
		buttonPanel.add(exportButton);
		buttonPanel.add(checkButton);

		// Add the combobox

		DefaultComboBoxModel cbmodel = new DefaultComboBoxModel();
		for (int i = 0; i < doc.getNumberOfSkillFields(); i++)
			cbmodel.insertElementAt(doc.getSkillName(i), i);
		final JComboBox comboBox = new JComboBox(cbmodel);

		comboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (table.isEditing())
				{
					table.getCellEditor().stopCellEditing();
				}
				allocateModel.setDisplaySkill(comboBox.getSelectedIndex());
				table.getChildCellRenderer().setDisplaySkill(
						comboBox.getSelectedIndex());
				allocateModel.fireTableDataChanged();
				allocateModel.fireTableChanged(new TableModelEvent(
						allocateModel, TableModelEvent.HEADER_ROW));
				table.formatColumns();
			}
		});

		buttonPanel.add(Box.createRigidArea(new Dimension(40, 0)));
		buttonPanel.add(new JLabel(Local.get("SkillLabel")));
		buttonPanel.add(comboBox);
		if (doc.getNumberOfSkillFields() > 0)
			comboBox.setSelectedIndex(0);

		ButtonGroup group = new ButtonGroup();
		final JRadioButton rb0 = new JRadioButton("Monday");
		rb0.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb0.isSelected())
					showNightFlag = 0;
				allocateModel.fireTableDataChanged();
			}
		});
		final JRadioButton rb1 = new JRadioButton("Tuesday");
		rb1.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb1.isSelected())
					showNightFlag = 1;
				allocateModel.fireTableDataChanged();
			}
		});
		final JRadioButton rb2 = new JRadioButton("Wednesday");
		rb2.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb2.isSelected())
					showNightFlag = 2;
				allocateModel.fireTableDataChanged();
			}
		});
		final JRadioButton rb3 = new JRadioButton("Thursday");
		rb3.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb3.isSelected())
					showNightFlag = 3;
				allocateModel.fireTableDataChanged();
			}
		});
		final JRadioButton rb4 = new JRadioButton("Friday");
		rb4.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb4.isSelected())
					showNightFlag = 4;
				allocateModel.fireTableDataChanged();
			}
		});
		final JRadioButton rb5 = new JRadioButton("Off", true);
		rb5.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e)
			{
				if (rb5.isSelected())
					showNightFlag = -1;
				allocateModel.fireTableDataChanged();
			}
		});

		group.add(rb0);
		group.add(rb1);
		group.add(rb2);
		group.add(rb3);
		group.add(rb4);
		group.add(rb5);
		JPanel bottomPanel = new JPanel();
		bottomPanel.add(rb0);
		bottomPanel.add(rb1);
		bottomPanel.add(rb2);
		bottomPanel.add(rb3);
		bottomPanel.add(rb4);
		bottomPanel.add(rb5);

		// the container
		
		this.setLayout(new BorderLayout());

		this.add(buttonPanel, BorderLayout.NORTH);
		this.add(bottomPanel, BorderLayout.SOUTH );
		this.add(tableScrollPane, BorderLayout.CENTER);
	}

	@Override
	protected void setButtonActions(JFrame parentFrame)
	{
		// TODO Auto-generated method stub

	}
}
